home *** CD-ROM | disk | FTP | other *** search
/ Aminet 16 / Aminet 16 (1996)(GTI - Schatztruhe)[!][Dec 1996].iso / Aminet / util / misc / cookietool.lha / cookietool / cdbsplit.c < prev    next >
C/C++ Source or Header  |  1996-10-03  |  6KB  |  222 lines

  1. ; /*
  2. gcc cdbsplit.c -O -noixemul -o cdbsplit
  3. quit 0 ; */
  4. /*************************************************************************\
  5.            cdbsplit: split your cookie database in two,
  6.          by keyword, by line length or by number of lines.
  7. Expected file format is plain text with a "%%" line ending each cookie. 
  8. Usage:
  9.     cdbsplit [options] infile [outfile1] [outfile2]
  10. options:           meaning:
  11.     -k<keywd>      search for a keyword
  12.     -K<keywd>      search for a keyword, case sensitive
  13.     -l<l_max>      only cookies with up to <l_max> lines
  14.     -w<w_max>      only cookies up to <w_max> chars wide
  15. Output file names default to <infile>.hit and <infile>.dump.
  16. If outfile1 but no outfile2 is specified, the dumpfile will overwrite
  17. the input file.
  18. \*************************************************************************/
  19.  
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <ctype.h>
  24.  
  25. char version[] = "$VER: cdbsplit 1.3 (03.10.96)";
  26.  
  27. #define CBUFSIZE 20000
  28. #define LBUFSIZE 2000
  29. char line[LBUFSIZE];    /* large enough to hold the longest line */
  30. char cbuf[CBUFSIZE];    /* large enough to hold one complete cookie */
  31. char cbak[CBUFSIZE];    /* backup after uppercase conversion */
  32. char uppercase[256];    /* conversion table */
  33.  
  34. int case_sense=0, l_min=0, l_max=0, w_min=0, w_max=0;
  35. char target[100];
  36.  
  37.  
  38. void help(char *s)
  39. /* print a help text and nag about illegal parameter <s> */
  40. {
  41.   if (s) printf("illegal option '%s'\n", s);
  42.   printf("usage:  cdbsplit <options> <cookiefile> [hitfile] [dumpfile] \n");
  43.   printf("where options are:\n");
  44.   printf(" -k<keyword>            search for a keyword\n");
  45.   printf(" -K<keyword>             \" , case sensitive\n");
  46.   printf(" -l<lines> / -L<lines>  range for number of lines in a cookie\n");
  47.   printf(" -w<width> / -W<width>  range for cookie line width\n");
  48. }
  49.  
  50. void init_tab()
  51. /* set up a conversion table that can convert all national characters from
  52.  * the ISO 8859-1/ECMA 94 charset to uppercase, not only 'a'-'z'.
  53.  */
  54. {
  55.   int c;
  56.   
  57.   for (c=0; c<256; c++)  uppercase[c] = c;
  58.   for (c='a'; c<='z'; c++)  uppercase[c] = toupper(c);
  59.   /* if (!ignore_iso) */
  60.   for (c=224; c<255; c++)   /* don't convert #255 ("y) !!! */
  61.     if (c != 247)     /* 247 is the division sign -:- */
  62.       uppercase[c] = c-32;
  63. }
  64.  
  65.  
  66. void makeupper(char *s)
  67. {
  68.   while(*s) {
  69.     *s = uppercase[(unsigned char) *s];
  70.     s++;
  71.   }
  72. }
  73.  
  74.  
  75. void filter_cookies(FILE *fp1, FILE *fp2, FILE *fp3)
  76. {
  77.   long count=0, hits=0, cbuflen;
  78.   int ok, lines, width, w;
  79.   char *printme;
  80.  
  81.   strcpy(cbuf,""); cbuflen = lines = width = 0;
  82.   while (fgets(line,LBUFSIZE,fp1)) {
  83.     if (strncmp(line,"%%",2)==0) {   /* "end of cookie"-marker */
  84.       if (!case_sense) {
  85.         strcpy(cbak, cbuf);
  86.         makeupper(cbuf); printme = cbak;
  87.       } else
  88.         printme = cbuf;
  89.       /* perform the checks: */
  90.       ok = (lines >= l_min) && (width >= w_min);
  91.       if (l_max)  ok = ok && (lines <= l_max);
  92.       if (w_max)  ok = ok && (width <= w_max);
  93.       if (target[0])  ok = ok && strstr(cbuf, target);
  94.       if (ok) {                 /* "good" cookie, copy it */
  95.         hits++;
  96.         fputs(printme, fp2);
  97.         fputs("%%\n", fp2);
  98.       } else if (fp3) {         /* copy others only if requested */
  99.         fputs(printme, fp3);
  100.         fputs("%%\n", fp3);
  101.       }
  102.       count++;
  103.       if (count%100 == 0) {
  104.         printf("Copying cookies, %ld hits, %ld misses.\r", hits, count-hits);
  105.         fflush(stdout);
  106.       }
  107.       strcpy(cbuf,"");        /* start a new cookie */
  108.       cbuflen = lines = width = 0;
  109.     } else {
  110.       w = strlen(line);
  111.       if ((cbuflen += w) >= CBUFSIZE) {
  112.         printf("\ncookie too big (>%ld chars)\n", CBUFSIZE);
  113.         exit(20);
  114.       }
  115.       strcat(cbuf,line); lines++;
  116.       if (w > width)  width = w;
  117.     }
  118.   }
  119.   printf("\nDone, %ld hits out of %ld.\n", hits, count);
  120. }
  121.  
  122.  
  123. int main(int argc, char *argv[])
  124. {
  125.   char *s;
  126.   char name1[100], name2[100], name3[100];
  127.   int writeback = 0;
  128.   FILE *infile, *hitfile, *dumpfile;
  129.   
  130.   name1[0] = name2[0] = name3[0] = target[0] = '\0';
  131.   if (argc < 3) {
  132.     help(NULL);
  133.     return 5;
  134.   }
  135.   while (--argc) {
  136.     s = *++argv;
  137.     if (*s != '-') {
  138.       if (name1[0] == '\0')
  139.         strcpy(name1, s);
  140.       else if (name2[0] == '\0')
  141.         strcpy(name2, s);
  142.       else
  143.         strcpy(name3, s);
  144.     } else {
  145.       switch (*++s) {
  146.         case 'k':
  147.           strcpy(target, ++s);
  148.           case_sense = 0; break;
  149.         case 'K':
  150.           strcpy(target, ++s);
  151.           case_sense = 1; break;
  152.         case 'l':
  153.           l_min = atoi(++s); break;
  154.         case 'L':
  155.           l_max = atoi(++s); break;
  156.         case 'w':
  157.           w_min = atoi(++s); break;
  158.         case 'W':
  159.           w_max = atoi(++s); break;
  160.         default:
  161.           help(argv[0]); return 5;
  162.       }
  163.     }
  164.   }
  165.   if (name1[0] == '\0') {
  166.     help(NULL);
  167.     return 5;
  168.   }
  169.   if (name2[0] == '\0') {
  170.     strcpy(name2,name1); strcat(name2,".hit");
  171.     strcpy(name3,name1); strcat(name3,".dump");
  172.   } 
  173.   if (name3[0] == '\0') {
  174.     strcpy(name3,"cdb_temp_kickme");
  175.     writeback = 1;
  176.   }
  177.   if (!(infile = fopen(name1,"r"))) {
  178.     printf("Can't open '%s' for input!\n", name1);
  179.     return 10;
  180.   }
  181.   if (writeback && (hitfile = fopen(name2,"r"))) {
  182.     printf("Error: shouldn't overwrite '%s'!\n", name2);
  183.     return 10;
  184.   }
  185.   if (!(hitfile = fopen(name2,"w"))) {
  186.     printf("Can't open '%s' for output!\n", name2);
  187.     return 10;
  188.   }
  189.   if (!(dumpfile = fopen(name3,"w"))) {
  190.     printf("Can't open '%s' for output!\n", name3);
  191.     return 10;
  192.   }
  193.   init_tab();
  194.   if (!case_sense)
  195.     makeupper(target);
  196.   if (writeback)
  197.     printf("Extracting from '%s' to '%s',\n", name1, name2);
  198.   else
  199.     printf("Splitting '%s' into '%s' and '%s',\n", name1, name2, name3);
  200.   if (target[0] != '\0')
  201.     printf("  search string is \"%s\".\n", target);
  202.   if (l_max)
  203.     printf("  looking for cookies %d - %d lines long.\n", l_min, l_max);
  204.   else if (l_min)
  205.     printf("  looking for cookies at least %d lines long.\n", l_min);
  206.   if (w_max)
  207.     printf("  looking for cookies %d - %d columns wide.\n", w_min, w_max);
  208.   else if (w_min)
  209.     printf("  looking for cookies at least %d columns wide.\n", w_min);
  210.   /* OK, here we go: */
  211.   filter_cookies(infile,hitfile,dumpfile);
  212.   fclose(infile); fclose(hitfile); fclose(dumpfile);
  213.   if (writeback) {
  214.     if (remove(name1) !=0 || rename(name3, name1) != 0) {
  215.       printf("Warning: couldn't overwrite the input file!\n");
  216.       return 5;
  217.     }
  218.   }
  219.   return 0;
  220. }
  221.  
  222.